package com.yd.app.mapper.provider;

import com.yd.domain.business.Analysis;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-04-24 9:32
 * @desc 统计分析
 **/
public class AnalysisProvider extends BaseProvider {
    //analysisByGStyle  Analysis analy
    /**
     * @Author: zy
     * @Desc: 根据商品类型进行统计
     * @Date:  2018/4/24
     * @Param
     */
    public String analysisByGStyle(Map map) {
        Analysis analy = (Analysis) map.get("analy");
        StringBuilder sb = new StringBuilder("SELECT gt.id,gt.typeName styleName,gy.typeName,u.companyName,a.dealWeight, COALESCE(SUM(v.dealNum),0) as dealNum, COALESCE(SUM(v.dealPrice),0) as dealPrice " +
                " FROM business_gstyle gt LEFT JOIN view_analysis v ON gt.id=v.gstyle  LEFT JOIN business_gtype gy on gt.pId=gy.id LEFT JOIN sys_user u on u.id=v.userId " +
                " LEFT JOIN (select va.gstyle gstyle,SUM((w.minWeight+w.maxWeight)*va.dealNum/2)  " +
                " as dealWeight from view_analysis va left join business_goods_weight w  on w.id=va.goodsWeightId " +
                " where va.status=1 GROUP BY va.gstyle  order by va.gstyle) a on a.gstyle=v.gstyle where 1=1 ");
        sb.append(searchAnaySql(analy));
        sb.append(" and v.status=1 GROUP BY gt.id  order by gt.pId ");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 根据商品材质进行统计
     * @Date:  2018/4/24
     * @Param
     */
    public String analysisByType(Map map) {
        Analysis analy = (Analysis) map.get("analy");
        StringBuilder sb = new StringBuilder("SELECT gt.id,gt.typeName, COALESCE(SUM(v.dealNum),0) as dealNum,COALESCE(SUM(v.dealPrice),0) as dealPrice " +
                " FROM business_gtype gt LEFT JOIN view_analysis v ON gt.id=v.type where 1=1  ");
        sb.append(searchAnaySql(analy));
        sb.append(" and gt.goodKind='1'  GROUP BY gt.id  ");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 查询合计件数及金钱
     * @Date:  2018/4/24
     * @Param
     */
    public String getSumByGStyle(Map map) {
        Analysis analy = (Analysis) map.get("analy");
        StringBuilder sb = new StringBuilder("SELECT  COALESCE(SUM(v.dealNum),0) as sumNum,COALESCE(SUM(v.dealPrice),0) as sumPrice " +
                " FROM business_gstyle gt LEFT JOIN view_analysis v ON gt.id=v.gstyle  LEFT JOIN sys_user u on u.id=v.userId  where 1=1 ");
        sb.append(searchAnaySql(analy));
        sb.append(" and v.status=1 ");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 统计分析查询条件
     * @Date:  2018/4/24
     * @Param
     */
    public String searchAnaySql(Analysis analy) {
        StringBuilder sb = new StringBuilder();
        if (!StringUtils.isEmpty(analy.getStartTime())) {
            sb.append(" AND v.dealTime >= #{analy.startTime}  ");
        }
        if (!StringUtils.isEmpty(analy.getEndTime())) {
            sb.append(" AND v.dealTime <= #{analy.endTime}  ");
        }
        if(!StringUtils.isEmpty(analy.getCompanyName())){
            sb.append(" and u.companyName like  '%"+analy.getCompanyName()+"%'  ");
        }
        return sb.toString();
    }
}
